public class demo25 {
    public static int ret1=0;
    public static int ret2=0;
    public static int sum=0;
    public static void main(String[] args) throws InterruptedException {
        long starttime=System.currentTimeMillis();
        Object locker=new Object();
        int[] array=new int[100000];
        for(int i=0;i<100000;i++){
            array[i]=(int)(Math.random()*100)+1;
        }
        Thread t1=new Thread(()->{
            for(int i=0;i<100000;i+=2){
                synchronized(locker){
                    ret1=ret1+array[i];
                }
            }
        });
        Thread t2=new Thread(()->{
            for(int i=1;i<100000;i+=2){
                synchronized(locker){
                    ret2=ret2+array[i];
                }
            }
        });
        t1.start();
        t2.start();
        t1.join();
        t2.join();
        System.out.println(ret1);
        System.out.println(ret2);
        System.out.println(ret1+ret2);
        long endtime=System.currentTimeMillis();
        long time=endtime-starttime;
        System.out.println("执行时间"+time);
    }
}
